from redis import *

def t_str():
    sr = StrictRedis(db=0)
    try:

        result = sr.set('name','xiaoming')
        print(result)
    except Exception as e:
        print(e)

    try:
        result = sr.get('name')
        print(result)
    except Exception as e:
        print(e)

    try:
        result = sr.set('name','itcast')
        print(result)
    except Exception as e:
        print(e)

    try:
        result = sr.delete('name')
        print(result)
    except Exception as e:
        print(e)
# t_str()
print("=================================")

def t_hash():
    sr = StrictRedis(db=1)
    try: # 增
        result = sr.hset('user','name','itheima')
        print(result)
    except Exception as e:
        print(e)

    try: # 查询
        result = sr.hget('user','name')
        print(result)
    except Exception as e:
        print(e)

    try: # 改
        result = sr.hset('user','name','itcast')
        print(result)
    except Exception as e:
        print(e)

    try: # 删除
        result = sr.hdel('user','name')
        print(result)
    except Exception as e:
        print(e)


# t_hash()
print("=========================")

def t_list():
    sr = StrictRedis(db=2)
    try: # 增
        result = sr.lpush('a1',1,2,3,4)
        print(result)
    except Exception as e:
        print(e)

    try: # 查
        result = sr.lrange('a1',0, -1)
        print(result)
    except Exception as e:
        print(e)

    # try : # 改
    #     result = sr.linsert('a1','before',2 ,6)
    #     print(result)
    # except Exception as e:
    #     print(e)

    try: # 删除
        result = sr.lrem('a1',5,6)
        print(result)
    except Exception as e:
        print(e)
# t_list()
print("===============================")

def t_set():
    sr = StrictRedis(db=3)
    try: # 增
        result = sr.sadd('name','zhangsan','lisi','wangwu')
        print(result)
    except Exception as e:
        print(e)

    try: # 查
        result = sr.smembers('name')
        print(result)
    except Exception as e:
        print(e)

    try: # 删
        result = sr.srem('name','wangwu')
        print(result)
    except Exception as e:
        print(e)

# t_set()
print("===========")

def t_zset():
    sr = StrictRedis(db=4)
    # try: # 增
    #     result = sr.zadd('name', {'lisi': '2', 'wangwu': '5', 'zhangsan': '8'})
    #     print(result)
    # except Exception as e:
    #     print(e)

    try: # 查
        result = sr.zrange('name',0,-1)
        print(result)
    except Exception as e:
        print(e)

    try: # 删
        result = sr.zrem('name','zhangsan')
        print(result)
    except Exception as e:
        print(e)

#　t_zset()